package cn.com.dhcc.turbo.service.exch.filedata;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.dao.CrudDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.service.AbstractCrudService;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.pub.core.consts.AppConst;
import cn.com.dhcc.app.pub.core.consts.NodeInfoParam;
import cn.com.dhcc.app.pub.core.tag.AppTagEl;
import cn.com.dhcc.turbo.dao.exch.SendFileDataDao;
import cn.com.dhcc.turbo.entity.exch.SendFileData;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.service.registry.BaseInfoService;

@Service
public class SendFileDataService extends AbstractCrudService<SendFileData> {
    @SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.R);

    @Autowired
    private SendFileDataDao dao;
    @Autowired
	private BaseInfoService baseInfoService;
    @Override
    public CrudDao<SendFileData> getCrudDao() {
        return dao;
    }


   /**
    * 
    * @param num 取多少条
    * @return
    * @throws ServiceException
    */
    public List<SendFileData> getUnsentLocalSysList(int num) throws ServiceException {
    	NodeInfo localNode=baseInfoService.getCurrentNodeInfo();
		boolean flag=AppTagEl.isInnerNet(NodeInfoParam.NODE_NET_TYPE);
        WhereCondition parameter = new WhereCondition();
        if (flag) {//内网
        	parameter.where()
            .leftBracket()
            .eq("SEND_STATUS", AppConst.SendStatus.READY.value())
            .and().eq("cross_net", AppConst.CrossNetType.SAME_NETWORK.value())
            .and().eq("cross_node", AppConst.NO)
             .rightBracket()
             .or().leftBracket()
             .eq("SEND_STATUS", AppConst.SendStatus.ANALYZE_SUCESS.value())
             .and().notEq("cross_net", AppConst.CrossNetType.SAME_NETWORK.value())
              .and().eq("RECV_NODECODE",localNode.getNodeCode())
             .rightBracket();
		}else{
			parameter.where()
	        .leftBracket()
	        .eq("SEND_STATUS", AppConst.SendStatus.READY.value())
	        .and().eq("cross_net", AppConst.CrossNetType.SAME_NETWORK.value())
	        .and().eq("cross_node", AppConst.NO)
	         .rightBracket()
	         .or().leftBracket()
	         .eq("SEND_STATUS", AppConst.SendStatus.ANALYZE_SUCESS.value())
	         .and().notEq("cross_net", AppConst.CrossNetType.SAME_NETWORK.value())
	          .and().eq("cross_node", AppConst.NO)
	         .rightBracket();
		}
        Pager pager=new Pager(0, num);
    	return this.getPagerList(pager, parameter).getDatas();
    }
    /**
     * 根据pid获取获取文件列表
     *
     * @return
     * @throws cn.com.dhcc.app.core.exception.NiceException
     */
    public List<SendFileData> getListByGroupId(String pid) throws ServiceException {
        WhereCondition parameter = new WhereCondition();
        parameter.where().eq("group_id", pid);
        return this.list(parameter);
    }

    /**
     * 获取尚未发送的本地系统列表
     * @return
     * @throws ServiceException
     */
    public List<SendFileData> getUnsentLocalSysList() throws ServiceException {
    	NodeInfo localNode=baseInfoService.getCurrentNodeInfo();
		boolean flag=AppTagEl.isInnerNet(NodeInfoParam.NODE_NET_TYPE);
        WhereCondition parameter = new WhereCondition();
        
        if (flag) {//内网
        	parameter.where()
            .leftBracket()
            .eq("SEND_STATUS", AppConst.SendStatus.READY.value())
            .and().eq("cross_net", AppConst.CrossNetType.SAME_NETWORK.value())
            .and().eq("cross_node", AppConst.NO)
             .rightBracket()
             .or().leftBracket()
             .eq("SEND_STATUS", AppConst.SendStatus.ANALYZE_SUCESS.value())
             .and().notEq("cross_net", AppConst.CrossNetType.SAME_NETWORK.value())
              .and().eq("RECV_NODECODE",localNode.getNodeCode())
             .rightBracket();
		}else{
			parameter.where()
	        .leftBracket()
	        .eq("SEND_STATUS", AppConst.SendStatus.READY.value())
	        .and().eq("cross_net", AppConst.CrossNetType.SAME_NETWORK.value())
	        .and().eq("cross_node", AppConst.NO)
	         .rightBracket()
	         .or().leftBracket()
	         .eq("SEND_STATUS", AppConst.SendStatus.ANALYZE_SUCESS.value())
	         .and().notEq("cross_net", AppConst.CrossNetType.SAME_NETWORK.value())
	          .and().eq("cross_node", AppConst.NO)
	         .rightBracket();
		}
        
        return this.list(parameter);
    }

	/**
	 * 获取本节点待发送的数据
	 * 内网到外网
	 * @return
	 * @throws NiceException
	 * @throws ServiceException
	 */
	public List<SendFileData> getSendOutNetWorkList() throws NiceException,ServiceException {
		WhereCondition parameter = new WhereCondition();
		parameter.where()
		     .leftBracket()
		     .eq("SEND_STATUS", AppConst.SendStatus.READY.value())
			.and().eq("CROSS_NET", AppConst.CrossNetType.INNER_OUT_NETWORK.value())
			.rightBracket()
			.or()
			.leftBracket().eq("SEND_STATUS", AppConst.SendStatus.READY.value())
			.and().eq("CROSS_NET", AppConst.CrossNetType.OUT_INNER_NETWORK.value())
			.and().eq("through_Gap", "2")
			.rightBracket();
		return this.list(parameter);
	}

	
	
	/**
	 * 获取本节点待发送的数据
	 * 外网到内网
	 * @return
	 * @throws NiceException
	 * @throws ServiceException
	 */
	public List<SendFileData> getSendInnerNetWorkList() throws NiceException,ServiceException {
		WhereCondition parameter = new WhereCondition();
		parameter.where().eq("SEND_STATUS", AppConst.SendStatus.READY.value())
			.and().eq("CROSS_NET", AppConst.CrossNetType.OUT_INNER_NETWORK.value());
		return this.list(parameter);
	}
	
	
	  /**
     * 获取尚未发送的数据列表
     * 数据
     * 不跨网，跨节点
     * 或者 内网到外网 跨节点 跨节点
     * @return
     * @throws cn.com.dhcc.app.core.exception.NiceException
     */
    public List<SendFileData> getUnsentList(int num) throws ServiceException {
    	
    	NodeInfo localNode=baseInfoService.getCurrentNodeInfo();
        WhereCondition parameter = new WhereCondition();
        parameter.where()
        .leftBracket()
        .eq("SEND_STATUS", AppConst.SendStatus.READY.value())
        .and().eq("cross_net", AppConst.CrossNetType.SAME_NETWORK.value())
        .and().eq("cross_node", AppConst.YES)
        .rightBracket()
        .or()
        .leftBracket()
        .eq("SEND_STATUS", AppConst.SendStatus.ANALYZE_SUCESS.value())
        .and().eq("cross_net", AppConst.CrossNetType.INNER_OUT_NETWORK.value())
        .and().eq("cross_node", AppConst.YES)
        .rightBracket()
        .or()
        .leftBracket()
        .eq("SEND_STATUS", AppConst.SendStatus.ANALYZE_SUCESS.value())
        .and().eq("cross_net", AppConst.CrossNetType.OUT_INNER_NETWORK.value())
        .and().notEq("RECV_NODECODE",localNode.getNodeCode())
        .rightBracket();
        Pager pager=new Pager(0, num);
        
        List<SendFileData> list=this.getPagerList(pager, parameter).getDatas();
        if (list==null) {
        	list=new ArrayList<SendFileData>();
		}
        return list;
    }
    
    
    /**
     * 获取尚未发送的数据列表
     * 数据
     * 不跨网，跨节点
     * 或者 内网到外网 跨节点 跨节点
     * @return
     * @throws cn.com.dhcc.app.core.exception.NiceException
     */
    public List<SendFileData> getUnsentList() throws ServiceException {
    	NodeInfo localNode=baseInfoService.getCurrentNodeInfo();
        WhereCondition parameter = new WhereCondition();
        parameter.where()
        .leftBracket()
        .eq("SEND_STATUS", AppConst.SendStatus.READY.value())
        .and().eq("cross_net", AppConst.CrossNetType.SAME_NETWORK.value())
        .and().eq("cross_node", AppConst.YES)
        .rightBracket()
        .or()
        .leftBracket()
        .eq("SEND_STATUS", AppConst.SendStatus.ANALYZE_SUCESS.value())
        .and().eq("cross_net", AppConst.CrossNetType.INNER_OUT_NETWORK.value())
        .and().eq("cross_node", AppConst.YES)
        .rightBracket()
        .or()
        .leftBracket()
        .eq("SEND_STATUS", AppConst.SendStatus.ANALYZE_SUCESS.value())
        .and().eq("cross_net", AppConst.CrossNetType.OUT_INNER_NETWORK.value())
        .and().notEq("RECV_NODECODE",localNode.getNodeCode())
        .rightBracket();
        return this.list(parameter);
    }

    /**
     * 获取尚未通知的数据列表
     *
     * @return
     * @throws cn.com.dhcc.app.core.exception.NiceException
     */
    public List<SendFileData> getUnnotifyList() throws ServiceException {

        WhereCondition parameter = new WhereCondition();

        parameter.where().eq("NOTIFY_STATUS", AppConst.NotifyStatus.READY.value())
                .and()
                .leftBracket()
                .eq("SEND_STATUS", AppConst.SendStatus.SENT.value()).or().eq("SEND_STATUS", AppConst.SendStatus.FAIL.value())
                .rightBracket()
                .and().eq("cross_net", AppConst.CrossNetType.SAME_NETWORK.value());
        return this.list(parameter);
    }

    /**
     * 批量更新数据发送状态
     *
     * @param dataList
     */
    public int updateSendStatus(List<SendFileData> dataList, String status) throws NiceException {
        try {
            return dao.updateSendStatus(dataList, status);
        } catch (DaoException e) {
            logger.error(e);
            throw new NiceException(e);
        }
    }

    /**
     * 批量更新数据通知状态
     *
     * @param dataList
     */
    public int updateNotifyStatus(List<SendFileData> dataList, String status) throws NiceException {
        try {
            return dao.updateNotifyStatus(dataList, status);
        } catch (DaoException e) {
            logger.error(e);
            throw new NiceException(e);
        }
    }

    public String getSendCount(String syscode, Date begintime, Date endtime) throws NiceException {

        try {
            return dao.getSendCount(syscode, begintime, endtime);
        } catch (DaoException e) {
            logger.error(e);
            throw new NiceException(e);
        }
    }


    public String getSendWait(String syscode) throws NiceException {
        try {
            return dao.getSendWait(syscode);
        } catch (DaoException e) {
            logger.error(e);
            throw new NiceException(e);
        }
    }

    public String getSendSending(String syscode) throws NiceException {
        try {
            return dao.getSendSending(syscode);
        } catch (DaoException e) {
            logger.error(e);
            throw new NiceException(e);
        }
    }

    public String getSendSuccess(String syscode) throws NiceException {
        try {
            return dao.getSendSuccess(syscode);
        } catch (DaoException e) {
            logger.error(e);
            throw new NiceException(e);
        }
    }

    public String getSendFail(String syscode) throws NiceException {
        try {
            return dao.getSendFail(syscode);
        } catch (DaoException e) {
            logger.error(e);
            throw new NiceException(e);
        }
    }
    
    /**
	 * 获取发送节点列表
	 * 
	 * @return
	 */
	public List<Map<String, Object>> getSendNodeList() throws NiceException {
		try {
			return dao.getSendNodeList();
		} catch (DaoException e) {
			logger.error(e);
			throw new NiceException(e);
		}
	}

	/**
	 * 获取接收节点列表
	 * 
	 * @return
	 */
	public List<Map<String, Object>> getRecvNodeList() throws NiceException {
		try {
			return dao.getRecvNodeList();
		} catch (DaoException e) {
			logger.error(e);
			throw new NiceException(e);
		}
	}

	/**
	 * 获取发送适配器列表
	 * 
	 * @return
	 */
	public List<Map<String, Object>> getSendAdapterList() throws NiceException {
		try {
			return dao.getSendAdapterList();
		} catch (DaoException e) {
			logger.error(e);
			throw new NiceException(e);
		}
	}
	
}
